<script>

$("#role-group").selectpicker({title:"请选择"});
$("#edit-role-group").selectpicker({title:"请选择"});



$(document).ready(function () {
    //添加用户
    $("#add-user-sub").click(function () {
        var username = $("#username").val();
        var nickname = $("#nickname").val();
        var password = $("#password").val();
        var is_super = $("#is-super").val();
        var status = $("#status").val();
        var email = $("#email").val();
        var mobile = $("#mobile").val();
        var role_ids = JSON.stringify($("#role-group").val());
        var aes_password = AesEncode(password);
        $.ajax({
            type:"POST",
            async:false,
            url:"/rbac/adduser/",
            data:{"username":username,"nickname":nickname,"password":aes_password,"passwd_key":ace_key_str,"is_super":is_super,
            "status":status,"email":email,"mobile":mobile,"role_ids":role_ids},
            dataType : "json",
            success:function (data) {
                $("#addUserModal").modal('hide');
                spop({
                    template: data,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout("location.reload()",2100);
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });

    //获取用户修改内容
    $("td>a[name='edit-user']").click(function () {
        var user_id = $(this).attr("userid");
        $.ajax({
            type: "POST",
            async: false,
            url: "/rbac/iduser/",
            data:{"user_id":user_id},
            dataType: "json",
            success:function (data) {
                var roles = data.Roles;
                var  role_ids = [];
                for (i=0;i<roles.length;i++){
                    role_ids.push(roles[i].ID);
                }
                //设置默认值
                $("#edit-role-group").val(role_ids);
                //刷新选项框显示
                $("#edit-role-group").selectpicker('refresh');
                $("#edit-username").val(data.UserName);
                $("#edit-nickname").val(data.NickName);
                //$("#edit-password").val(data.UserPwd);
                $("#edit-is-super").val(String(data.IsSuper));
                $("#edit-status").val(data.Status);
                $("#edit-email").val(data.Email);
                $("#edit-mobile").val(data.Mobile);
                $("#edit-user-sub").attr("user_id",data.ID);
                $("#editUserModal").modal("show");
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        });

    });

    //提交用户修改内容
    $("#edit-user-sub").click(function () {
        var user_id = $(this).attr("user_id");
        var username = $("#edit-username").val();
        var nickname = $("#edit-nickname").val();
        var password = $("#edit-password").val();
        var is_super = $("#edit-is-super").val();
        var status = $("#edit-status").val();
        var email = $("#edit-email").val();
        var mobile = $("#edit-mobile").val();
        var role_ids = JSON.stringify($("#edit-role-group").val());
        if (password != ""){
            var aes_password = AesEncode(password);
        }else{
            var aes_password = ""
        }

        $.ajax({
            type:"PUT",
            async:false,
            url:"/rbac/edituser/",
            data:{"user_id":user_id,"username":username,"nickname":nickname,"password":aes_password,"passwd_key":ace_key_str,"is_super":is_super,
                "status":status,"email":email,"mobile":mobile,"role_ids":role_ids},
            dataType : "json",
            success:function (data) {
                $("#editUserModal").modal('hide');
                spop({
                    template: data,
                    style: 'success',
                    autoclose: 2000
                });
                setTimeout("location.reload()",2100);
            },
            error:function (data) {
                spop({
                    template:data.responseText,
                    style: 'error',
                    autoclose: 2000
                });
            }
        })
    });
    //删除用户
    $("td>a[name='del-user']").click(function () {
        var user_id = $(this).attr('userid');
        var statu = confirm("是否确认删除!");
        if (statu==true)
        {
            $.ajax({
                type: "post",
                async:false,
                url: "/rbac/deluser/",
                data: {"user_id":user_id},
                dataType : "json",
                success: function(data) {
                    spop({
                        template:data,
                        style: 'warning',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            });
        }
    });
    //搜索用户
    $("#search-user").bind('change',function () {
        var key = $(this).val();
        if (key != "" ) {
            var url = "/rbac/searchuser/" + key + "/";

        }else{
            var url = "/rbac/user/";
        }
        $("#search-user-a").attr("href", url);
    });


    $("td>a[name='change-pwd']").click(function () {
        var user_id = $(this).attr("userid");
        $("#change-pwd-sub").attr("user_id",user_id);
        $("#changepwdModal").modal("show");
    });

    //修改用户密码
    $("#change-pwd-sub").click(function () {
        var user_id = $(this).attr("user_id");
        var password = $("#change-pwd").val();
        var password_confirm = $("#change-pwd").val();
        if (password != "" && password == password_confirm){
            var aes_password = AesEncode(password);
            $.ajax({
                type:"POST",
                async:false,
                url:"/rbac/changepwd/",
                data:{"user_id":user_id,"password":aes_password,"passwd_key":ace_key_str},
                dataType : "json",
                success:function (data) {
                    $("#changepwdModal").modal('hide');
                    spop({
                        template: data,
                        style: 'success',
                        autoclose: 2000
                    });
                    setTimeout("location.reload()",2100);
                },
                error:function (data) {
                    spop({
                        template:data.responseText,
                        style: 'error',
                        autoclose: 2000
                    });
                }
            })
        }

    });


})
</script>
